--This is init.lua

--Download these files
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "player.lua" )
AddCSLuaFile( "vgui/cl_playermodel.lua" )
AddCSLuaFile( "vgui/cl_votemap.lua" )
AddCSLuaFile( "vgui/cl_welcome.lua" )
AddCSLuaFile( "cl_deathnotice.lua" )

resource.AddFile( "materials/vgui/nwsIcon.vtf" )
resource.AddFile( "materials/vgui/nwsIcon.vmt" )
resource.AddFile( "models/Dodgeball.mdl" )
resource.AddFile( "materials/models/Dodgeball.vtf" )
resource.AddFile( "materials/models/Dodgeball.vmt" )
resource.AddFile( "models/weapons/v_throwingarm.mdl" )
resource.AddFile( "materials/particles/blue_star.vtf" )
resource.AddFile( "materials/particles/blue_star.vmt" )

--Load up these files
include( 'shared.lua' )
include( 'player.lua' )
include( 'round_control.lua' )
include( 'utility.lua' )
include( 'player_info.lua' )
include( 'sv_concommands.lua' )
include( 'effects.lua' )
--Make a couple of tables containing the player models
--PlayerModelsDirectory just shortened the length of PlayerModelsLambda
PlayerModelsDir = "models/player/"
PlayerModelsLambda = { "Group03/female_01.mdl", "Group03/female_02.mdl", "Group03/female_03.mdl", "Group03/female_04.mdl", 
	"Group03/female_06.mdl", "Group03/female_07.mdl", "Group03/male_01.mdl", "Group03/male_02.mdl", "Group03/male_03.mdl", 
	"Group03/male_04.mdl", "Group03/male_05.mdl", "Group03/male_06.mdl", "Group03/male_07.mdl", "Group03/male_08.mdl", 
	"Group03/male_09.mdl" }
PlayerModelsCombine = { "Combine_Soldier.mdl", "Combine_Soldier_PrisonGuard.mdl", "Combine_Super_Soldier.mdl", "Police.mdl" }

function GM:Initialize()
	--The initialization message
	MsgAll( "*----------------------------------------------------*\n" )
	MsgAll( "*            Garry's Mod Dodgeball Loaded!           *\n" )
	MsgAll( "*                     V. 1.3.1 Beta                  *\n" )
	MsgAll( "*----------------------------------------------------*\n" )
	//This replaces the need for the GM.<var here> and adds a little flexibility
	CreateConVar( "db_time_round", 60, FCVAR_NOTIFY )
	CreateConVar( "db_time_delay_between_rounds", 5, FCVAR_NOTIFY )
	CreateConVar( "db_time_warmup", 30, FCVAR_NOTIFY )
	CreateConVar( "db_warmup_each_round", 1, FCVAR_NOTIFY )
	CreateConVar( "db_rounds_per_map", 5, FCVAR_NOTIFY )
	CreateConVar( "db_time_map_vote", 15, FCVAR_NOTIFY )
	CreateConVar( "db_time_score_show", 10, FCVAR_NOTIFY )
	CreateConVar( "db_dev", 0, FCVAR_NOTIFY )
	CreateConVar( "db_forced_maps", "", FCVAR_NOTIFY )
end

Stat_Update_Time = CurTime( ) + 0.25
RoundState = 0
--0: Initial loading: Right after startup or map change
--1: Round Warmup: A free for all part to wait for players
--2: Begin the round
--3: End round, display scores / reset or changemap
--4: Pick next map to play
TimeDone = false
RoundTime = 0
DelayTime = 0
RoundNumber = 1

BallSPawnPoints = {}

function GM:Think( )

	--This controls the rounds
	if ( RoundState == 0 ) then
		On_Round_Init( )
	elseif ( RoundState == 1 ) then
		--This is the Warm Up to allow time for other players to recconect after a map switch.
		--Not really used for much else.
		--The players will immidiatley respawn if they are killed.
		--If the timer is done and the number of players in the active teams is greater than 1
		if ( TimeDone == true and team.NumPlayers( TEAM_COMBINE ) + team.NumPlayers( TEAM_LAMBDA ) > 0 ) then
			On_Warm_Up_Over( )
		end
	elseif ( RoundState == 2 ) then
		--This is the actual round, if hit you are out for the game!
		if ( TimeDone == true ) then -- Also check if one of the teams are out of players
			Save_All_Player_Data( )
			On_Round_Over( false )
		elseif ( Teams_Check_Out( ) ) then
			On_Round_Over( true )
		end
	elseif ( RoundState == 3 ) then
		--This is the end of the round part, displays scores
		if ( TimeDone == true ) then
			On_Scores_Over( )
		end
	elseif ( RoundState == 4 ) then
		if (TimeDone == true ) then
			On_Vote_Map_Over( )
		end
	end
	--Update the user's hud info
	if ( Stat_Update_Time < CurTime( ) ) then
		umsg.Start( "SendRoundStats" )
			umsg.String( RoundState )
			umsg.String( RoundNumber )
			umsg.String( RoundTime )
			umsg.String( DelayTime )
		umsg.End( )
		Stat_Update_Time = CurTime( ) + 0.25
	end
end

--Press F4
function GM:ShowSpare2( ply )
	umsg.Start( "vote_for_next_map", pl )
		umsg.String( string.Implode(",", GAMEMODE.maps) )
	umsg.End( )
end

--Press F3
function GM:ShowSpare1( ply )
	ply:SendLua( "GAMEMODE:ShowSpare1( )" )
end

--Press F2
function GM:ShowTeam( ply )
	ply:SendLua( "GAMEMODE:ShowTeam( )" )
end

--Press F1
function GM:ShowHelp( ply )
	ply:SendLua( "GAMEMODE:ShowTeam( )" ) 
end